package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Servlet18", value = "/Servlet18")
public class Servlet18 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获取referer头
        String referrer = request.getHeader("referer");
        String sitePart = "http://" + request.getServerName();
        if (referrer != null && referrer.startsWith(sitePart)) {
            // 处理下载请求
            out.println("正在下载 ...");
        } else {
            // 非法下载请求转发到本站的下载说明页
            RequestDispatcher rd = request.getRequestDispatcher("/downError.html"); rd.forward(request, response);
        }
    }
}
